package cn.zh.npro.Activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;

import cn.zh.npro.Item.OrItem;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.Loading;
import cn.zh.npro.Util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class OrDetail extends BaseActivity {

    ImageView mc_back;
    LinearLayout orstate, clientlin;
    OrItem orItem = null;
    TextView ortye, ornum, orstime, cliname, total, pnum, custype, gtype, r;
    ImageView call;
    TextView readress, rtxt, rename, retime;
    String CustomerID;
    JSONArray Trade_GoodsList = null;
    LinearLayout gdslin;
    TextView agtxt;
    TextView confirmlin;
    private Dialog LDialog;
    MyApplication mapp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_or_detail);
        mapp = (MyApplication) getApplication();
        orItem = (OrItem) getIntent().getSerializableExtra("object");
        initof();
        getOrded();
    }

    public void initof() {
        mc_back = findViewById(R.id.mc_back);
        call = findViewById(R.id.call);
        orstate = findViewById(R.id.orstate);
        clientlin = findViewById(R.id.clientlin);
        gdslin = findViewById(R.id.gdslin);
        confirmlin = findViewById(R.id.confirmlin);
        ortye = findViewById(R.id.ortye);
        agtxt = findViewById(R.id.agtxt);
        ornum = findViewById(R.id.ornum);
        orstime = findViewById(R.id.orstime);
        cliname = findViewById(R.id.cliname);
        total = findViewById(R.id.total);
        pnum = findViewById(R.id.pnum);
        custype = findViewById(R.id.custype);
        gtype = findViewById(R.id.gtype);
        readress = findViewById(R.id.readress);
        rtxt = findViewById(R.id.rtxt);
        rename = findViewById(R.id.rename);
        retime = findViewById(R.id.retime);
        r = findViewById(R.id.r);
        call.setOnClickListener(this);
        ortye.setText(orItem.getTradeStatus());

        ornum.setText("订单号:" + orItem.getTradeNO());
        String stim = orItem.getTradeTime();
        int index = stim.indexOf(".");
        if (-1 == index) {
        } else {
            stim = stim.substring(0, index);
        }
        stim = stim.replace("T", " ");
        orstime.setText("下单时间:" + stim);
        cliname.setText(orItem.getCustomerName());
        DecimalFormat df = new DecimalFormat("#0.00");
        total.setText("¥" + df.format(orItem.getGoodsTotal()));

        mc_back.setOnClickListener(this);
        orstate.setOnClickListener(this);
        clientlin.setOnClickListener(this);
        gdslin.setOnClickListener(this);
        confirmlin.setOnClickListener(this);
        agtxt.setOnClickListener(this);
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        switch (id) {
            case R.id.mc_back:
                finish();
                break;
            case R.id.orstate:
                Intent intent = new Intent(OrDetail.this, OrState.class);
                startActivity(intent);
                break;
            case R.id.call:
                String num = pnum.getText().toString();
                Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + num));
                startActivity(dialIntent);
                break;
            case R.id.clientlin:
                Intent intent1 = new Intent(OrDetail.this, Client.class);
                intent1.putExtra("clId", CustomerID);
                intent1.putExtra("type", "ortype");
                startActivity(intent1);
                break;
            case R.id.gdslin:
                if (null == Trade_GoodsList) {
                    ToastUtil.showLong(OrDetail.this, "暂无数据");
                    return;
                }
                System.out.println("------------>商品清单===" + Trade_GoodsList.toString());
                Intent intent2 = new Intent(OrDetail.this, OrGoods.class);
                intent2.putExtra("ja", Trade_GoodsList.toString());
                startActivity(intent2);
                break;
            case R.id.agtxt:
                mapp.setTraId(orItem.getTradeID());
                Intent intent3 = new Intent(OrDetail.this, RepOd.class);
                intent3.putExtra("type", "1");
                intent3.putExtra("ob", orItem);
                intent3.putExtra("clId", CustomerID);
                startActivity(intent3);
                break;
            case R.id.confirmlin:
                GetConfirmOrder();
        }

    }


    public void getOrded() {
        OkHttpUtils.get(BaseUrl.GetTradeDetail)
                .params("tradeId", orItem.getTradeID())
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("------------>获取订单详情===" + s);
                        try {
                            JSONObject js = new JSONObject(s);
                            boolean State = js.getBoolean("State");
                            if (State) {
                                JSONObject Res = js.getJSONObject("Res");
                                JSONObject Trade_TradeList = Res.getJSONObject("Trade_TradeList");
                                CustomerID = Trade_TradeList.getString("CustomerID");
                                String CustomerLevel = Trade_TradeList.getString("CustomerLevel");
                                String SndTo = Trade_TradeList.getString("SndTo");
                                String Adr = Trade_TradeList.getString("Adr");
                                String Remark = Trade_TradeList.getString("Remark");
                                String ortype = Trade_TradeList.getString("TradeStatus");
                                String WuliuAdr = Trade_TradeList.getString("WuliuAdr");
                                String YSndTime = Trade_TradeList.getString("YSndTime");
                                String Tel = Trade_TradeList.getString("Tel");
                                readress.setText(Adr);
                                r.setText(Remark);
                                pnum.setText(Tel);
                                rtxt.setText(WuliuAdr);
                                String stim = YSndTime;
                                int index = stim.indexOf(".");
                                if (-1 == index) {
                                } else {
                                    stim = stim.substring(0, index);
                                }
                                stim = stim.replace("T", " ");
                                retime.setText(stim);
                                rename.setText(SndTo);
                                custype.setText(CustomerLevel);
                                String GoodsCount = Res.getString("GoodsCount");
                                gtype.setText(GoodsCount + "种");
                                Trade_GoodsList = Res.getJSONArray("Trade_GoodsList");

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }

    private void GetConfirmOrder() {
        String tradeId = orItem.getTradeID();
        MyApplication mapp = (MyApplication) getApplication();
        SharedPreferences sp = mapp.getSp();
        String userId = sp.getString("uid", "");
        String ortype = orItem.getTradeStatus();
        OkHttpUtils.post(BaseUrl.GetConfirmOrder)
                .params("tradeId", tradeId)
                .params("userId", userId)
                .execute(new StringCallback() {

                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        Loading.closeDialog(LDialog);
                        try {
                            JSONObject js = new JSONObject(s);
                            boolean State = js.getBoolean("State");
                            String Msg = js.getString("Msg");
                            ToastUtil.showShort(OrDetail.this, Msg);
                            if (State) {
                                confirmlin.setVisibility(View.GONE);
                                Intent intent = new Intent(OrDetail.this, MainActivity.class);
                                startActivity(intent);

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }


}
